<%@page import="org.lzy.pt.bean.PluginType"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.util.HashMap"%>
<%@page import="org.lzy.pt.PluginManager"%>
<%@ page import="java.util.Map"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<%
	boolean another = false;
	boolean create = false;
	boolean cancel = false;

	String T_Name = null;
	String T_Ver = null;
	String T_Path = null;
	byte[] T_Data = null;
	
	Map<String, String> errors = new HashMap<String, String>();

	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	if (isMultipart) {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		List items = upload.parseRequest(request);

		for (Object item : items) {
			FileItem fileItem = (FileItem) item;
			if (fileItem.isFormField()) {
				if (fileItem.getFieldName().equals("T_Name")) {
					T_Name = fileItem.getString();
				}
				if (fileItem.getFieldName().equals("T_Ver")) {
					T_Ver = fileItem.getString();
				}
				if (fileItem.getFieldName().equals("T_Path")) {
					T_Path = fileItem.getString();
				}
				if (fileItem.getFieldName().equals("create")) {
					create = true;
				}
				if (fileItem.getFieldName().equals("another")) {
					another = true;
					create = true;
				}
				if (fileItem.getFieldName().equals("cancel")) {
					cancel = true;
				}

			}
			if (fileItem.getFieldName().equals("T_Data")) {
				T_Data = fileItem.get();
			}
		}

		// Handle a cancel
		if (cancel) {
			response.sendRedirect("plugin-summary.jsp");
			return;
		}

		// Handle a request to create a user:
		if (create) {
			// Validate

			if (T_Name == null) {
				errors.put("T_Name", "");
			}
			if (T_Ver == null) {
				errors.put("T_Ver", "");
			}

			// do a create if there were no errors
			if (errors.size() == 0) {
				try {

					PluginManager.getInstance().addPlugin(T_Name,
							Integer.parseInt(T_Ver), T_Path, T_Data);

					if (another) {
						response.sendRedirect("plugin-create.jsp?success=true");
					} else {
						response.sendRedirect("plugin-summary.jsp");
					}
					return;
				} catch (Exception e) {
					errors.put("general", "");
				}
			}
		}
	}
%>

<html>
    <head>
        <title><fmt:message key="plugin.create.title"/></title>
        <meta name="pageID" content="plugin-create"/>
    </head>
    <body>

<p><fmt:message key="plugin.create.info" /></p>

<%
	if (!errors.isEmpty()) {
%>

    <div class="jive-error">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr>
            <td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0" alt=""/></td>
            <td class="jive-icon-label">

            <%
            	if (errors.get("general") != null) {
            %>
                <fmt:message key="plugin.create.error_creating_plugin" />
            <%
            	} else if (errors.get("T_Name") != null) {
            %>
            	<fmt:message key="plugin.T_Name.error_null" />
            <%
            	} else if (errors.get("T_Ver") != null) {
            %>
            	<fmt:message key="plugin.T_Ver.error_null" />	
            <%
	            	} else {
	            %>
            	<fmt:message key="error.unknown" />
            <%
            	}
            %>
            </td>
        </tr>
    </tbody>
    </table>
    </div>
    <br>

<%
	} else if (request.getParameter("success") != null) {
%>

    <div class="jive-success">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0" alt=""></td>
        <td class="jive-icon-label">
        <fmt:message key="plugin.create.created_success" />
        </td></tr>
    </tbody>
    </table>
    </div><br>

<%
	}
%>

<form name="f" action="plugin-create.jsp" method="post" enctype="multipart/form-data">

	<div class="jive-contentBoxHeader">
		<fmt:message key="plugin.create.new_plugin" />
	</div>
	<div class="jive-contentBox">
		<table cellpadding="3" cellspacing="0" border="0">
		<tbody>
		<tr>
			<td width="1%" nowrap><label for="T_Name"><fmt:message key="plugin.T_Name" />:</label> *</td>
			<td width="99%">
				
				<%
					List<PluginType> pluginTypes = PluginManager.getInstance().getPluginTypeList();
				%>
				
				<select size="1" name="T_Name" >
				    <% for (PluginType pluginType : pluginTypes) { %>
					    <option value="<%=pluginType.getT_Name() %>" >
					    	<%=pluginType.getT_CnName() %>
					    </option>
				    <% } %>
				
				</select>
			
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Ver"><fmt:message key="plugin.T_Ver" />:</label>*</td>
			<td width="99%">
				<input type="text" name="T_Ver" size="30" maxlength="75" value="<%=((T_Ver != null) ? T_Ver : "")%>"
				 id="T_Ver"/>
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Path"><fmt:message key="plugin.T_Path" />:</label></td>
			<td width="99%">
				<input type="text" name="T_Path" size="30" maxlength="75" value="<%=((T_Path != null) ? T_Path : "")%>"
				 id="T_Path"/>
			</td>
		</tr>
		<tr>
			<td nowrap><label for="T_Data"><fmt:message key="plugin.T_Data" />:</label></td>
			<td width="99%">
<%-- 				<input type="text" name="T_Data" size="30" maxlength="75" value="<%=((T_Data != null) ? T_Data : "")%>" --%>
<!-- 				 id="T_Data"> -->
					<input type="file" name="T_Data" size="30" maxlength="75" id="T_Data" />
			</td>
		</tr>
        <tr>
			<td colspan="2" style="padding-top: 10px;">
				<input type="submit" name="create" value="<fmt:message key="plugin.create.create" />">
				<input type="submit" name="another" value="<fmt:message key="plugin.create.create_another" />">
				<input type="submit" name="cancel" value="<fmt:message key="global.cancel" />"></td>
		</tr>
		</tbody>
		</table>

	</div>

	<span class="jive-description">
    * <fmt:message key="user.create.requied" />
    </span>
</form>

<script language="JavaScript" type="text/javascript">
document.f.T_Name.focus();
</script>
    </body>
</html>